<template>
  <div>
    <n-modal
      v-model:show="showModal"
      :mask-closable="false"
      :show-icon="false"
      preset="dialog"
      transform-origin="center"
      title="物流信息"
      :style="{
        width: dialogWidth,
      }"
    >
      <n-scrollbar style="max-height: 87vh" class="pr-5">
        <n-spin :show="loading" description="请稍候...">
          <n-form
            ref="formRef"
            :model="formValue"
            :rules="rules"
            :label-placement="settingStore.isMobile ? 'top' : 'left'"
            :label-width="100"
            class="py-4"
          >
            <n-grid cols="2 s:2 m:2 l:2 xl:2 2xl:2" responsive="screen">
              <n-gi span="1">
                <n-form-item label="收货人：" path="recipient">
                  {{ formValue.recipient }}
                </n-form-item>
              </n-gi>
              <n-gi span="1">
                <n-form-item label="手机号：" path="tel">
                  {{ formValue.tel }}
                </n-form-item>
              </n-gi>
              <n-gi span="2">
                <n-form-item label="地区：" path="shippingProvinceName">
                  {{ formValue.shippingProvinceName }}
                </n-form-item>
              </n-gi>
              <n-gi span="2">
                <n-form-item label="详细地址：" path="shippingAddress">
                  {{ formValue.shippingAddress }}
                </n-form-item>
              </n-gi>
                <n-gi span="2">
                <n-form-item label="状态：" path="status">
                  {{ formValue.status === 1 ? '待发货' : '已发货' }}
                </n-form-item>
              </n-gi>
              <n-gi span="2">
                <n-form-item label="物流公司" path="expressCompany">
                  <n-select v-model:value="formValue.expressCompany" :options="dict.getOptionUnRef('ExpressOptions')" />
                </n-form-item>
              </n-gi>
              <n-gi span="2">
                <n-form-item label="快递单号" path="ExpressNumber">
                  <n-input placeholder="请输入快递单号" v-model:value="formValue.expressNumber" />
                </n-form-item>
              </n-gi>
            </n-grid>
          </n-form>
        </n-spin>
      </n-scrollbar>
      <template #action>
        <n-space>
          <n-button @click="closeForm">
            取消
          </n-button>
          <n-button type="info" :loading="formBtnLoading" @click="confirmForm">
            {{ formValue.status === 1 ? '确认发货' : '保存' }}
          </n-button>
        </n-space>
      </template>
    </n-modal>
  </div>
</template>

<script lang="ts" setup>
  import { Edit, View } from '@/api/expressInfo';
import { useDictStore } from '@/store/modules/dict';
import { useProjectSettingStore } from '@/store/modules/projectSetting';
import { adaModalWidth } from '@/utils/hotgo';
import { useMessage } from 'naive-ui';
import { computed, ref } from 'vue';
import { State, newState, rules } from './model';

  const emit = defineEmits(['reloadTable']);
  const message = useMessage();
  const settingStore = useProjectSettingStore();
  const dict = useDictStore();
  const loading = ref(false);
  const showModal = ref(false);
  const formValue = ref<State>(newState(null));
  const formRef = ref<any>({});
  const orderStatus = ref();
  const formBtnLoading = ref(false);
  const dialogWidth = computed(() => {
    return adaModalWidth(840);
  });

  // 提交表单
  function confirmForm(e) {
    e.preventDefault();
    if (formValue.value.status === 1) {
      formValue.value.status = 2
      formValue.value.isConfirm = true
    }
    formRef.value.validate((errors) => {
      if (!errors) {
        formBtnLoading.value = true;
        Edit(formValue.value)
          .then((_res) => {
            message.success('操作成功');
            closeForm();
            emit('reloadTable');
          })
          .finally(() => {
            formBtnLoading.value = false;
          });
      } else {
        message.error('请填写完整信息');
      }
    });
  }

  // 关闭表单
  function closeForm() {
    showModal.value = false;
    loading.value = false;
  }

  // 打开模态框
  function openModal(state: State) {
    showModal.value = true;

    // 新增
    if (!state || state.id < 1) {
      formValue.value = newState(state);

      return;
    }

    orderStatus.value = state.status
    console.log( orderStatus.value);
    
    // 编辑
    loading.value = true;
    View({ 
      id: state.expressInfoId
    })
      .then((res) => {
        formValue.value = res;
        if (formValue.value.expressCompany === 0) {
          formValue.value.expressCompany = null;
        }
      })
      .finally(() => {
        loading.value = false;
      });
  }

  defineExpose({
    openModal,
  });
</script>

<style lang="less"></style>
